package com.starrtc.demo.imclient.clientHandler;

import com.starrtc.demo.imclient.ClientSender.LoginSender;
import com.starrtc.demo.imclient.iMcommon.bean.msg.ProtoMsg;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

@ChannelHandler.Sharable
public class ChatMsgHandler extends ChannelInboundHandlerAdapter {

    public static ChatMsgHandler instance;

    public static ChatMsgHandler getInstance(){
        if(instance == null){
            instance = new ChatMsgHandler(LoginSender.getInstance());
        }
        return instance;
    }

    private ChatMsgHandler(LoginSender sender) {
    }


    /**
     * 业务逻辑处理
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //判断消息实例
        if (null == msg || !(msg instanceof ProtoMsg.Message)) {
            super.channelRead(ctx, msg);
            return;
        }

        //判断类型
        ProtoMsg.Message pkg = (ProtoMsg.Message) msg;
        ProtoMsg.HeadType headType = pkg.getType();
        if (!headType.equals(ProtoMsg.HeadType.MESSAGE_REQUEST)) {
            super.channelRead(ctx, msg);
            return;
        }

        ProtoMsg.MessageRequest req= pkg.getMessageRequest();
        String content=req.getContent();
        String uid=req.getFrom();

        System.out.println(" 收到消息 from uid:" + uid + " -> " + content);
  }


}
